#include "Camera.h"
#include "Level.h"
#include "Character.h"

Camera::Camera(Level* level) : Rect<int>(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT) {
    levelWidth   = level->getWidth();
    levelHeight  = level->getHeight();
    background   = level->background;
    foreground   = level->foreground;
    characters   = &level->characters;
    collidables  = &level->collidables;
}

void Camera::move(int x, int y ) {
    // Update background/foreground subrects
    Left  += x;
    Top   += y;
    Right  = Left + WINDOW_WIDTH;
    Bottom = Top  + WINDOW_HEIGHT;

    background->SetSubRect(*this);
    foreground->SetSubRect(*this);
    
    // Update characters positions (don't move the hero character)
    for (vector<Character*>::iterator i = characters->begin() + 1; i != characters->end(); i++) {
        (*i)->getSprite().Move(-x, -y);
    }
    
    // Update collidable object positions
    for (vector<Sprite*>::iterator i = collidables->begin(); i != collidables->end(); i++) {
        (*i)->Move(-x, -y);
    }
}
